//
//  MyDrawView.m
//  TQLSmartPen
//
//  Created by tql on 2018/5/28.
//  Copyright © 2018年 tqlZj. All rights reserved.
//

#import "MyDrawView.h"
@interface MyDrawView()
@property (nonatomic, strong) NSMutableArray *pointArray;
@property (nonatomic, strong) NSMutableArray *pageArray;
@property (nonatomic, assign)int penWidth;
//@property (nonatomic , assign)BOOL isEnd;
@end

@implementation MyDrawView

- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        _isChangePage = NO;
        _strokeColor =[UIColor blackColor];
        _penWidth = 1;
        _hasDraw = NO;
      
    }
    return self;
}

-(void)dealloc{
    
    
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
- (NSMutableArray *)pointArray{
    if (!_pointArray) {
        
        _pointArray = [NSMutableArray array];
    }
    return _pointArray;
}

-(NSMutableArray *)pageArray{
    if (!_pageArray) {
        _pageArray = [NSMutableArray array];
    }
    return _pageArray;
}


- (void)startWithScale:(float)scale
               offsetX:(float)offsetX
               offsetY:(float)offsetY
              penWidth:(int)penWidth
                     x:(float)pointX
                     y:(float)pointY
                 force:(int)force{
    
//    if (_isEnd) {
//        [self end];
//        _isEnd = NO;
//    }
    if (_isChangePage) {
        _hasDraw = YES;
        //创建小数组
        NSMutableArray *array = [NSMutableArray array];
        //数组的第一个元素是颜色
        [array addObject:self.strokeColor];
        //第二个元素是画笔的粗细
        [array addObject:[NSNumber numberWithInt:penWidth]];
        CGPoint point = CGPointMake(pointX, pointY);
        //把cgpoint 转化为对象 放入数组
        NSValue *pointValue = [NSValue valueWithCGPoint:point];
       // [self.pointArray addObject:pointValue];
         [array addObject:pointValue];
        //小数组放入大数组
         [self.pageArray addObject:array];
   }
   else{
   _hasDraw = YES;
//    //创建小数组
//    //数组的第一个元素是颜色
    _penWidth = penWidth;
//    //第二个元素是画笔的粗细
       NSMutableArray *array = [NSMutableArray array];
       //数组的第一个元素是颜色
   
    //获取触摸对象
      CGPoint point = CGPointMake(pointX, pointY);
    //把cgpoint 转化为对象 放入数组
      NSValue *pointValue = [NSValue valueWithCGPoint:point];
      [array addObject:pointValue];
    [self.pointArray addObject:array];
    }
}
- (void)moveWithScale:(float)scale
              offsetX:(float)offsetX
              offsetY:(float)offsetY
             penWidth:(int)penWidth
                    x:(float)pointX
                    y:(float)pointY
                force:(int)force{
    if (_isChangePage) {
        //获取触摸对象
       
        //获取移动的点
        CGPoint point = CGPointMake(pointX, pointY);
        NSValue *moveValue = [NSValue valueWithCGPoint:point];
       // _penWidth = penWidth;
        //获取移动的最后一个小数组
        NSMutableArray *arr = [_pageArray lastObject];
        //将移动的点加到数组里面
        [arr addObject:moveValue];
        //执行重绘操作
        [self setNeedsDisplay];
  }
    else{

    //获取移动的点
    CGPoint point = CGPointMake(pointX, pointY);
    NSValue *moveValue = [NSValue valueWithCGPoint:point];
    NSMutableArray *arr = [_pointArray lastObject];
        //将移动的点加到数组里面
    [arr addObject:moveValue];
    //执行重绘操作
    [self setNeedsDisplay];
   }
    
}


-(void)endWithScale:(float)scale
            offsetX:(float)offsetX
            offsetY:(float)offsetY
           penWidth:(int)penWidth
                  x:(float)pointX
                  y:(float)pointY
              force:(int)pressValue{
    if (_isChangePage) {
        
        CGPoint point = CGPointMake(pointX, pointY);
        NSValue *moveValue = [NSValue valueWithCGPoint:point];
       // _penWidth = penWidth;
        //获取移动的最后一个小数组
        NSMutableArray *arr = [_pageArray lastObject];
        //将移动的点加到数组里面
        [arr addObject:moveValue];
        //执行重绘操作
        [self setNeedsDisplay];
        
   }else{

    CGPoint point = CGPointMake(pointX, pointY);
    NSValue *moveValue = [NSValue valueWithCGPoint:point];
    NSMutableArray *arr = [_pointArray lastObject];
       //将移动的点加到数组里面
       [arr addObject:moveValue];
       //_isEnd = YES;
    //执行重绘操作
    [self setNeedsDisplay];
}
}
- (void)end{
    
    if (_hasDraw){
        
       // dispatch_async(dispatch_get_main_queue(), ^{
            
        [self snapshotViewFromRect:self.bounds withCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        //});
       // [self.pointArray removeAllObjects];
    }
}


- (void)snapshotViewFromRect:(CGRect)rect withCapInsets:(UIEdgeInsets)capInsets {

    //UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);

    //CGContextRef currentContext = UIGraphicsGetCurrentContext();

//    CGContextTranslateCTM(currentContext, - CGRectGetMinX(rect), - CGRectGetMinY(rect));
//    [self.layer renderInContext:currentContext];
//    UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    //UIImageView *snapshotView = [[UIImageView alloc] initWithFrame:rect];
//   UIImage* image = [snapshotImage resizableImageWithCapInsets:capInsets];

   // [_pointArray removeAllObjects];
   // [self setNeedsDisplay];
    _hasDraw = NO;
    //return snapshotView.image;
   // UIImage *newImage = [self imageWithImage:image scaledToSize:self.picSize];
    if (self.degelete) {
        
        [self.degelete getDrawImage:nil];
    }
    if (_isChangePage) {
        
        [self.pageArray removeAllObjects];
        _isChangePage = NO;
    }
}

//-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
//    // Create a graphics image context
//    UIGraphicsBeginImageContext(newSize);
//    // Tell the old image to draw in this new context, with the desired
//    // new size
//    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
//
//    // Get the new image from the context
//    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
//
//    // End the context
//    UIGraphicsEndImageContext();
//
//    // Return the new image.
//    return newImage;
//}


/*- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    //设置颜色
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextSetFillColorSpace(context, colorSpace);
    CGContextSetStrokeColorSpace(context, colorSpace);
    // CGContextSetAllowsAntialiasing(context, YES);
    CGContextSetBlendMode(context, kCGBlendModeCopy);
 
    
    if (_isChangePage) {
        
         for(int i = 0; i < [_pageArray count]; i++)
        {
            
            NSMutableArray *arr = [NSMutableArray arrayWithArray:_pageArray[i]];
             //获取上下文(就是画板)
           
        if (arr.count > 2 && arr) {
            //设置颜色
            //设置画笔数组的粗细
            UIColor *color = [arr objectAtIndex:0];
            int width = [[arr objectAtIndex:1] intValue];
             CGContextSetStrokeColorWithColor(context, color.CGColor);
            CGContextSetLineWidth(context, width);
            //设置每一笔的起点
            NSValue *pointValue = [arr objectAtIndex:2];
            CGPoint point = [pointValue CGPointValue];
            //起点
            CGContextMoveToPoint(context, point.x, point.y);
            //具体每一笔
            
            for (int i = 3;i < arr.count; i++) {
                
                NSValue *pointValue1 = [arr objectAtIndex:i];
                CGPoint point1 = [pointValue1 CGPointValue];
                CGContextAddLineToPoint(context, point1.x, point1.y);
            }
            CGContextStrokePath(context);
        }
        }
    }
    else{
       
        
       if (_pointArray.count <= 0) {
           return;
       }
            NSMutableArray *arr = [_pointArray lastObject];
       // }
          //
            if (arr.count > 2 && arr) {
               // NSMutableArray *writeArray = [NSMutableArray array];
               // [writeArray addObjectsFromArray:arr];
                // //获取上下文(就是画板)
                //设置每一笔的起点
                CGContextSetStrokeColorWithColor(context, self.strokeColor.CGColor);
                //设置画笔数组的粗细
                //float w = [[array objectAtIndex:1]floatValue];
                CGContextSetLineWidth(context, _penWidth);
                if (arr) {
                    NSValue *pointValue = [arr objectAtIndex:0];
                    CGPoint point = [pointValue CGPointValue];
                    //起点
                    CGContextMoveToPoint(context, point.x, point.y);
                    //具体每一笔
                    for (int i = 1; i < arr.count; i++) {
                         if (arr) {
                        NSValue *pointValue1 = arr[i];
                        CGPoint point1 = [pointValue1 CGPointValue];
                        CGContextAddLineToPoint(context, point1.x, point1.y);
                         }
                        
                    }
                }
               
                //CGContextFillPath(context);
                CGContextStrokePath(context);
               
        }
    }
}
*/
- (void)drawRect:(CGRect)rect {
    
    if (_isChangePage) {
        for(int i = 0; i < [_pageArray count]; i++)
        {
            NSMutableArray *arr = _pageArray[i];
            //获取上下文(就是画板)
            
            if (arr.count > 2) {
                //设置颜色
                CGContextRef context = UIGraphicsGetCurrentContext();
                UIColor *color = [arr objectAtIndex:0];
                int width = [[arr objectAtIndex:1] intValue];
                CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
                CGContextSetFillColorSpace(context, colorSpace);
                CGContextSetStrokeColorSpace(context, colorSpace);
                //CGContextSetAllowsAntialiasing(context, YES);
                CGContextSetBlendMode(context, kCGBlendModeCopy);
                CGContextSetStrokeColorWithColor(context, color.CGColor);
                CGContextSetLineCap(context, kCGLineCapRound);
                CGContextSetLineJoin(context, kCGLineJoinRound);
                
                //设置画笔数组的粗细
                CGContextSetLineWidth(context, width);
                //设置每一笔的起点
                NSValue *pointValue = [arr objectAtIndex:2];
                CGPoint point = [pointValue CGPointValue];
                //起点
                CGContextMoveToPoint(context, point.x, point.y);
                //具体每一笔
                for (int i = 3;i < arr.count; i++) {
                    NSValue *pointValue1 = [arr objectAtIndex:i];
                    CGPoint point1 = [pointValue1 CGPointValue];
                    CGContextAddLineToPoint(context, point1.x, point1.y);
                }
                CGContextStrokePath(context);
            }
        }
    }
    else{
        
        NSArray *arr = [_pointArray lastObject];
        //
        if (arr.count > 2) {
            
            // //获取上下文(就是画板)
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetLineCap(context, kCGLineCapRound);
            CGContextSetLineJoin(context, kCGLineJoinRound);
            //设置颜色
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
            CGContextSetFillColorSpace(context, colorSpace);
            CGContextSetStrokeColorSpace(context, colorSpace);
            // CGContextSetAllowsAntialiasing(context, YES);
            CGContextSetBlendMode(context, kCGBlendModeCopy);
            CGContextSetStrokeColorWithColor(context, self.strokeColor.CGColor);
            //设置画笔数组的粗细
            //float w = [[array objectAtIndex:1]floatValue];
            CGContextSetLineWidth(context, _penWidth);
            //设置每一笔的起点
            NSValue *pointValue = [arr objectAtIndex:0];
            
            CGPoint point = [pointValue CGPointValue];
            //起点
            CGContextMoveToPoint(context, point.x, point.y);
            
            //具体每一笔
            for (int i = 1; i < arr.count; i++) {
                
                NSValue *pointValue1 = [arr objectAtIndex:i];
                CGPoint point1 = [pointValue1 CGPointValue];
                
                CGContextAddLineToPoint(context, point1.x, point1.y);
            }
            //CGContextFillPath(context);
            CGContextStrokePath(context);
            
        }
    }
}

- (void)erase{
    [_pageArray removeAllObjects];
    [_pointArray removeAllObjects];
    [self setNeedsDisplay];
    
}




@end
